<%@ page pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<html>

<head><title>Login Success</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    websocket
    <input type="button" value="发送消息"  onclick="send()" />
    <input type="text"  id="txt" />
</body>
</html> 
<script type="text/javascript">
// 创建WebSocket连接:
var ws = new WebSocket('ws://' + location.host + '/chat');
// 连接成功时:
ws.addEventListener('open', function (event) {
    console.log('websocket connected.');
});
// 收到消息时:服务器数据可能是文本，也可能是 二进制数据（blob对象或Arraybuffer对象）。
ws.addEventListener('message', function (event) {
    console.log('message: ' + event.data);
    if(typeof event.data === String) {
        console.log("Received data string");
    }    
    if(event.data instanceof ArrayBuffer){
        var buffer = event.data;
        console.log("Received arraybuffer");
    }
    //var msgs = JSON.parse(event.data);
    // TODO:
});
// 连接关闭时:
ws.addEventListener('close', function () {
    console.log('websocket closed.');
});
// 绑定到全局变量:
window.chatWs = ws;

function send(){
    window.chatWs.send(document.getElementById('txt').value);
    document.getElementById('txt').value="";
}
</script>